import { makeAutoObservable } from 'mobx';
import { inject, injectable } from 'inversify';
import { RoleListModel } from '../models/role.list.model.ts';
import { UpdateRoleUseCase } from '../usecase/updateRoleUseCase.ts';
import { DeleteRoleUseCase } from '../usecase/deleteRoleUseCase.ts';
import { CreateRoleUseCase } from '../usecase/createRoleUseCase.ts';
import { requestWOMeta } from '../data/user.roles.dto.ts';
import { RolesListTable } from '../view/UserRolesPage/interface.ts';

@injectable()
export class RoleListViewModel {
  get loading(): boolean {
    return this.roleListModel.loading;
  }

  get permissionLoading(): boolean {
    return this.roleListModel.permissionLoading;
  }

  get roles(): RolesListTable[] {
    return this.roleListModel.roles.map((item) => {
      return {
        id: item._id,
        name: item.name,
        description: item.description,
        type: item.type,
        isActive: item.isActive,
      };
    });
  }

  constructor(
    @inject(RoleListModel)
    private roleListModel: RoleListModel,
    @inject(UpdateRoleUseCase) private updateRoleUseCase: UpdateRoleUseCase,
    @inject(DeleteRoleUseCase) private deleteRoleUseCase: DeleteRoleUseCase,
    @inject(CreateRoleUseCase) private createRoleUseCase: CreateRoleUseCase,
  ) {
    makeAutoObservable(this);
  }

  async createRole(params: requestWOMeta) {
    await this.createRoleUseCase.execute(params);
  }

  async updateRole(_id: string, params: requestWOMeta) {
    await this.updateRoleUseCase.execute(_id, params);
  }

  async deleteRole(_id: string) {
    await this.deleteRoleUseCase.execute(_id);
  }

  dispose() {
    this.roleListModel.dispose();
  }
}
